
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FactMonthSales]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
 drop table [dbo].[FactMonthSales]

GO

CREATE TABLE [dbo].[FactMonthSales] (
	[SalesDateMonthKey] [int] NOT NULL,
	[CommodityKey] [int] NOT NULL,
	[CommodityGroupKey] [int] NOT NULL,
	[DivisionKey] [int] NOT NULL,
	[CustomerKey] [int] NOT NULL,
	[CustomerDeliveryAddressKey] [int] NOT NULL,
	[CustomerBillingAddressKey] [int] NOT NULL,
	[MerchantKey] [int] NOT NULL,
	[SubMerchantKey] [int] NOT NULL,
	[DeliveryTypeKey] [int] NOT NULL,
	[Quantity] [int] NOT NULL,
	[LocalValue] [numeric] (18, 3) NOT NULL,
	[LocalValueVAT] [numeric] (18, 3) NOT NULL,
	[DeliveryValue] [numeric] (18, 3) NOT NULL,
	[DeliveryValueVAT] [numeric] (18, 3) NOT NULL,
	[LocalTotalValue] [numeric] (18, 3) NOT NULL,
	[DivisionTotalValue] [numeric] (18, 3) NOT NULL,
	[CorporateTotalValue] [numeric] (18, 3) NOT NULL,
	[IsUpdated] [bit] NOT NULL,

	CONSTRAINT [FK_FactMonthSales_SalesDateMonthKey] FOREIGN KEY ([SalesDateMonthKey]) REFERENCES [dbo].[DimDateMonth] ([DateMonthKey]) NOT FOR REPLICATION,

	CONSTRAINT [FK_FactMonthSales_CommodityKey] FOREIGN KEY ([CommodityKey]) REFERENCES [dbo].[DimCommodity] ([CommodityKey]) NOT FOR REPLICATION,

	CONSTRAINT [FK_FactMonthSales_CommodityGroupKey] FOREIGN KEY ([CommodityGroupKey]) REFERENCES [dbo].[DimCommodityGroup] ([CommodityGroupKey]) NOT FOR REPLICATION,

	CONSTRAINT [FK_FactMonthSales_DivisionKey] FOREIGN KEY ([DivisionKey]) REFERENCES [dbo].[DimDivision] ([DivisionKey]) NOT FOR REPLICATION,

	CONSTRAINT [FK_FactMonthSales_CustomerKey] FOREIGN KEY ([CustomerKey]) REFERENCES [dbo].[DimCustomer] ([CustomerKey]) NOT FOR REPLICATION,

	CONSTRAINT [FK_FactMonthSales_MerchantKey] FOREIGN KEY ([MerchantKey]) REFERENCES [dbo].[DimMerchant] ([MerchantKey]) NOT FOR REPLICATION,

	CONSTRAINT [FK_FactSales_SubMerchantKey] FOREIGN KEY ([SubMerchantKey]) REFERENCES [dbo].[DimSubMerchant] ([SubMerchantKey]) NOT FOR REPLICATION,

	CONSTRAINT [FK_FactSales_CustomerDeliveryAddressKey] FOREIGN KEY ([CustomerDeliveryAddressKey]) REFERENCES [dbo].[DimAddress] ([AddressKey]) NOT FOR REPLICATION,

	CONSTRAINT [FK_FactSales_CustomerBillingAddressKey] FOREIGN KEY ([CustomerBillingAddressKey]) REFERENCES [dbo].[DimAddress] ([AddressKey]) NOT FOR REPLICATION,

	CONSTRAINT [FK_FactSales_DeliveryTypeKey] FOREIGN KEY ([DeliveryTypeKey]) REFERENCES [dbo].[DimDeliveryType] ([DeliveryTypeKey]) NOT FOR REPLICATION
	
	

)

GO

grant references, select, insert, delete, update on [dbo].[FactMonthSales] to [db_datawriter]

GO

grant select on [dbo].[FactMonthSales] to [public]

GO

